Skip to content

refactor(datagrid): extract TableSelection value type#926

Merged
datlechin merged 1 commit intomainfrom
refactor/datagrid-phase4-selection
Apr 28, 2026
Merged

refactor(datagrid): extract TableSelection value type#926
datlechin merged 1 commit intomainfrom
refactor/datagrid-phase4-selection

Conversation

@datlechin
Copy link
Copy Markdown
Member

Summary

Phase A of the data grid clean-architecture refactor. Self-contained, low-risk: extracts the four cell-focus / range-selection ints from KeyHandlingTableView into a single TableSelection value type.

Why

KeyHandlingTableView owned four loose stored properties (focusedRow, focusedColumn, selectionAnchor, selectionPivot) plus two separate didSet blocks that each computed partial reload indices. The blocks were almost-but-not-quite redundant:

  • focusedRow.didSet reloaded only the OLD row, relying on selection-change to redraw the new row
  • focusedColumn.didSet reloaded BOTH columns at the current row

Inconsistent enough to be subtle. Hard to test (state lives on a UIView).

What changed

  • New TableSelection: Equatable value type (focusedRow, focusedColumn, anchor, pivot, plus setFocus, clearFocus, resetAnchor, clearAnchor, hasFocus)
  • TableSelection.reloadIndexes(from:) returns the (rows, columns) IndexSet pair needing redraw on a transition
  • KeyHandlingTableView holds one selection: TableSelection; its didSet calls reloadIndexes and reloads
  • The four old properties are kept as computed accessors that mutate selection (no churn at call sites)

Tests

17 unit cases for TableSelection covering: defaults, focus mutation, anchor/pivot lifecycle, equatable, and every transition path through reloadIndexes (no change, anchor-only, initial focus, clearing focus, row change, column change, both change).

Files

  • TablePro/Views/Results/TableSelection.swift (new)
  • TablePro/Views/Results/KeyHandlingTableView.swift (state migration)
  • TableProTests/Views/Results/TableSelectionTests.swift (new)
  • TableProTests/Views/Main/Child/DataTabGridDelegateTests.swift (FakeRowDeltaApplier conforms to existing invalidateCachesForUndoRedo requirement added in refactor(datagrid): interaction fixes, window UndoManager, multi-cell paste #924)

Test plan

  • Click a cell, arrow keys move focus, focus border follows
  • Shift+arrow extends row range from anchor
  • Click row number column clears focus
  • Click empty area clears focus
  • Tab/Shift+Tab navigates between cells
  • No regressions in undo/redo/edit (TableSelection only owns focus + range, not data)

…gTableView

Replace four stored properties (focusedRow, focusedColumn, selectionAnchor,
selectionPivot) with a single TableSelection struct. Centralized didSet
computes reload indices for transitioning focus, eliminating per-property
reload logic.

Backward-compat accessors keep external call sites unchanged. Phase A of
the data grid clean-architecture refactor.
@datlechin datlechin merged commit 5877d31 into main Apr 28, 2026
2 checks passed
@datlechin datlechin deleted the refactor/datagrid-phase4-selection branch April 28, 2026 08:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant